package com.wanxs.mall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wanxs.mall.common.service.impl.CrudServiceImpl;
import com.wanxs.mall.common.utils.PageUtils;
import com.wanxs.mall.common.utils.Query;
import com.wanxs.mall.product.dao.BrandDao;
import com.wanxs.mall.product.dto.BrandDTO;
import com.wanxs.mall.product.entity.BrandEntity;
import com.wanxs.mall.product.service.BrandService;
import com.wanxs.mall.product.service.CategoryBrandRelationService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/**
 * 品牌
 *
 * @author wanxs 130001934@qq.com
 * @since 1.0.0 2023-09-01
 */
@Service
public class BrandServiceImpl extends CrudServiceImpl<BrandDao, BrandEntity, BrandDTO> implements BrandService {


    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

    @Override
    public QueryWrapper<BrandEntity> getWrapper(Map<String, Object> params){
        String key = (String)params.get("key");
        QueryWrapper<BrandEntity> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(key)){
            // 添加对应的查询条件
            wrapper.eq("brand_id",key).or().like("name",key);
        }


        return wrapper;
    }


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        // 1. 获取key
        String key = (String)params.get("key");
        QueryWrapper<BrandEntity> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(key)){
            // 添加对应的查询条件
            wrapper.eq("brand_id",key).or().like("name",key);
        }
        IPage<BrandEntity> page = baseDao.selectPage(
                new Query<BrandEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }
    @Transactional
    @Override
    public void updateDetail(BrandEntity entity) {
        baseDao.updateById(entity);
        if(StringUtils.isNotEmpty(entity.getName())){
            categoryBrandRelationService.updateBrandName(entity.getBrandId(),entity.getName());
        }
    }


}